package userData.action;

import account.entity.AccountVO;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import userData.entity.UserDataVO;
import userData.service.UserDataService;
import util.FileInputOut;
import util.ObtainOther;

import java.io.File;

public class UserDataAction extends ActionSupport {

    private final static Log LOGER = LogFactory.getLog(UserDataAction.class);

    private final static String uploadDir =ObtainOther.obtainMessage("userImg");

    private UserDataService userDataService;

    private UserDataVO userDataVO;

    private AccountVO accountVO;

    private Integer userDataId;

    private String message;

    private File imgFile;

    private String imgFileType;

    public String getImgFileType() {
        return imgFileType;
    }

    public void setImgFileType(String imgFileType) {
        this.imgFileType = imgFileType;
    }

    public File getImgFile() {
        return imgFile;
    }

    public void setImgFile(File imgFile) {
        this.imgFile = imgFile;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public UserDataVO getUserDataVO() {
        return userDataVO;
    }

    public void setUserDataVO(UserDataVO userDataVO) {
        this.userDataVO = userDataVO;
    }

    public AccountVO getAccountVO() {
        return accountVO;
    }

    public void setAccountVO(AccountVO accountVO) {
        this.accountVO = accountVO;
    }

    public Integer getUserDataId() {
        return userDataId;
    }

    public void setUserDataId(Integer id) {
        this.userDataId = userDataId;
    }

    public UserDataService getUserDataService() {
        return userDataService;
    }

    public void setUserDataService(UserDataService userDataService) {
        this.userDataService = userDataService;
    }

    /**
     * 添加个人资料
     *
     * @return
     */
    public String addUserData() {
        try {
            accountVO = ObtainOther.obtainAccount();
            if (!("").equals(imgFileType)  && userDataVO.getUserImage() != null && imgFileType != null) {
                userDataVO.setUserImage(FileInputOut.fileInputOut(userDataVO.getUserImage(), imgFile, uploadDir));
            }
            userDataVO = userDataService.addUserDataVO(userDataVO, accountVO);

            return SUCCESS;
        } catch (Exception e) {
            message = message + util.message.getInstance().get("addUserDataError").toString();
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 更新个人资料
     *
     * @return
     */

    public String updateUserData() {
        try {
            if (!("").equals(imgFileType)  && userDataVO.getUserImage() != null && imgFileType != null) {
                userDataVO.setUserImage(FileInputOut.fileInputOut(userDataVO.getUserImage(), imgFile, uploadDir));
            }
            userDataVO = userDataService.updateUserDataVO(userDataVO);
            message = util.message.getInstance().get("updateUserDataSuccess").toString();
            return SUCCESS;
        } catch (Exception e) {
            message = util.message.getInstance().get("updateUserDataError").toString();
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    public String initUserData() {
        Integer userDataVoId = 0;
        try {
            userDataVoId = ObtainOther.obtainId();
            userDataVO = userDataService.findUserDataVOById(userDataVoId);
            if (userDataVO == null) {
                return INPUT;
            } else {
                return SUCCESS;
            }
        } catch (Exception e) {
            message = util.message.getInstance().get("loadUserDataError").toString();
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    public String loadUserData() {
        Integer userDataVoId = 0;
        try {
            userDataVoId = ObtainOther.obtainId();
            userDataVO = userDataService.findUserDataVOById(userDataVoId);
            return SUCCESS;
        } catch (Exception e) {
            message = util.message.getInstance().get("loadUserDataError").toString();
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

}
